<template>
  <div class="log-in-container">
    <div class="header"></div>
    <div class="container">
      <div class="form">
        <h2>登录</h2>
        <el-form
          ref="form"
          :model="user"
          hide-required-asterisk
          :rules="rules"
        >
          <el-form-item
            prop="username"
          >
            <el-input
                v-model="user.username"
                placeholder="账号"
                clearable
            />
          </el-form-item>
          <el-form-item
            prop="password"
          >
            <el-input
                v-model="user.password"
                placeholder="密码"
                show-password
                clearable
            />
          </el-form-item>
        </el-form>
        <p>
          <label>
            <input
              v-model="isRemembered"
              type="checkbox"
            />记住密码
          </label>
        </p>
        <button
          type="submit"
          class="btn"
          @click="login()"
        >登录</button>
          <p class="forget">没有账户?<a @click="register()">
            注册
          </a></p>
      </div>
    </div>
  </div>
</template>

<script>
import api from '@/api'

export default {
    name: 'LogIn',
    beforeRouteEnter(to, from, next) {
        next((vm) => vm.autoLogin())
    },
    data() {
        return {
            user: {
                username: '',
                password: ''
            },
            isRemembered: false,
            rules: {
                username: [{required:true, message: '账号不可为空', trigger: 'blur'}],
                password: [{required:true, message: '密码不可为空', trigger: 'blur'}]
            }
        }
    },
    methods: {
        async register() {
            await this.$router.push({name: 'register'})
        },
        autoLogin() {
            this.user.username = localStorage.getItem('userName')
            if(localStorage.getItem('isRemembered')) {
                this.user.password = localStorage.getItem('password')
                this.login()
            }
        },
        async login() {
            if (typeof this.user.username === 'undefined' || this.user.username === null) {
                return
            }
            if (typeof this.user.password === 'undefined' || this.user.password === null) {
                return
            }
            const resp = await api.login.login(this.user)
            if(resp.Code === 1400) {
                this.$notify.error({
                    title: '错误',
                    message: '密码不正确！'
                })
                return
            }
            localStorage.setItem('userName', this.user.username)
            this.$store.commit('setToken', resp.data)
            if(this.isRemembered) {
                localStorage.setItem('isRemembered', 'true')
                localStorage.setItem('password', this.user.password)
            }
            await this.$router.push({name: 'files'})
        }
    }
}
</script>

<style scoped lang="less">

.log-in-container {
  background-color: #1fc8db;
  background-image: linear-gradient(141deg,#9fb8ad 0%,#1fc8db 51%,#2cb5e8 75%);
  background-size: cover;
  background-position: center;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.container {
  position: relative;
  width: 400px;
  min-height: 400px;
  background: rgba(255, 255, 255, 0.1);
  display: flex;
  justify-content: center;
  align-items: center;
  backdrop-filter: blur(5px);
  box-shadow: 0 25px 45px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-right: 1px solid rgba(255, 255, 255, 0.2);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.form {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 30px;
}

.form h2 {
  position: relative;
  color: #fff;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: 5px;
  margin-bottom: 30px;
  cursor: pointer;
}

.form h2::before {
  content: "";
  position: absolute;
  left: 0;
  bottom: -10px;
  width: 0px;
  height: 3px;
  background: #fff;
  transition: 0.5s;
}

.form h2:hover:before {
  width: 53px;
}

.form .el-form-item{
  width: 100%;
  margin-top: 20px;
}

.form .btn {
  background: #fff;
  color: #666;
  max-width: 100px;
  margin-bottom: 10px;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  margin-top: 10px;
}

.forget {
  margin-top: 6px;
  color: #fff;
  letter-spacing: 1px;
}

.forget a {
  color: #fff;
  font-weight: 600;
  text-decoration: none;
}
</style>

<style lang="less">
.form .el-input__inner, .form .btn{
  width: 100%;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.2);
  outline: none;
  border-radius: 30px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-right: 1px solid rgba(255, 255, 255, 0.2);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  font-size: 16px;
  letter-spacing: 1px;
  color: #fff;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}

.form .el-input__inner::placeholder {
  color: #fff;
}
</style>